--require "MainScene"
ZhiHuiBuScene={}
ZhiHuiBuSceneFunc={}
ZhiHuiBuSceneFunc.__index = ZhiHuiBuSceneFunc
setmetatable(ZhiHuiBuScene, ZhiHuiBuSceneFunc)
ZhiHuiBuScene.NodeID = {UpButton=1020, ShouShuiButton=1021}
--ZhiHuiBuScene.upgradeCooldown = 100
ZhiHuiBuScene.shouShuiCooldown = 0

ZhiHuiBuSceneFunc.getNode = function()
  local l = {}
  setmetatable(l, ZhiHuiBuSceneFunc)
  l.node = CCBReader:load("ZhiHuiBuScene.ccbi")
  print("ZhiHuiBuScene.getNode", l.node)
  ZhiHuiBuScene.instance = l
  l:init() 
  return l 
end

ZhiHuiBuSceneFunc.getScene = function()
  local sc = CCScene:create()
  sc:addChild(ZhiHuiBuScene.getNode().node)
  print("ZhiHuiBuScene.getScene", sc)
  return sc
end

ZhiHuiBuSceneFunc.init = function(self)
  local b = self.node:getChildByTag(ZhiHuiBuScene.NodeID.UpButton)
  setObjectExtTable(b, self)
  b = self.node:getChildByTag(ZhiHuiBuScene.NodeID.ShouShuiButton)
  setObjectExtTable(b, self)
  b = self.node:getChildByTag(1111)
  setObjectExtTable(b, self)
  --TypeCast(self.node:getChildByTag(1020), "CCButton"):setTitle("test")
  --setBuildingBaseData(BuildingID.ZhiHuiBu, self.node)
  
  self.upBuildingButton = TypeCast(self.node:getChildByTag(1020), "CCButton")
  self.shoushuiButton = TypeCast(self.node:getChildByTag(1021), "CCButton")
  self.timeLabel = TypeCast(self.node:getChildByTag(1006), "CCLabelTTF")
  self.shoushuiTimeLabel = TypeCast(self.node:getChildByTag(1012), "CCLabelTTF")
  self.shoushuiCountLabel = TypeCast(self.node:getChildByTag(1013), "CCLabelTTF")
  self.curCashLabel = TypeCast(self.node:getChildByTag(1010), "CCLabelTTF")
  self.nextCashLabel = TypeCast(self.node:getChildByTag(1011), "CCLabelTTF")

  --[[
  self.shoushuiCountLabel:setString(string.format("今日收税(%d/%d)", UserData.userinfo.host.counter.harvestCashTimes, 10))
  local l = UserData.userinfo.host.cities2[tostring(BuildingID.ZhiHuiBu)].l 
  local levelConfig = UserData.config.models.BaseLevel[l]
  self.curCashLabel:setString(tostring(levelConfig.cashNumber))
  if UserData.config.models.BaseLevel[l+1]~=nil then
    levelConfig = UserData.config.models.BaseLevel[l+1]
    self.nextCashLabel:setString(tostring(levelConfig.cashNumber))
  end]]
  self:setLabels()
  --UpgradeManager.checkUpgrade(BuildingID.ZhiHuiBu)
  --self:setUpgradeCoolDown()
  --self:setShouShuiCoolDown()
  local tmp = UITimer.getUITimer(string.format("upbuildingcd_%d", BuildingID.ZhiHuiBu), self, ccp(160,72), ccp(0, 0))
  if tmp~=nil then
    tmp.view:setPosition(ccp(710, 420))
    self.node:addChild(tmp.view)
    self.upBuildingTimer = tmp
    self.timeLabel:setVisible(false)
    self.upBuildingButton:setVisible(false)
    self.upBuildingButton:setEnabled(false)
  end
  tmp = UITimer.getUITimer("shoushuicd", self, ccp(0,0), ccp(0, 0), true)
  if tmp~=nil then
    tmp.view:setPosition(ccp(357, 150))
    self.node:addChild(tmp.view)
    self.shoushuiTimer = tmp
    self.shoushuiTimeLabel:setVisible(false)
    self.shoushuiButton:setEnabled(false)
  end
  self:checkInvade()
end
ZhiHuiBuSceneFunc.checkInvade = function(self)
  local invader = UserData.userinfo.host.invader
  local l = self.node:getChildByTag(1030)
  if invader==0 or UserData.userinfo.host.releaseTime<TimeManager.getServerTime() then
    l:setVisible(false)
    setTouchEnabledAll(l, false)
    return
  else
    l:setVisible(true)
  end
  setObjectExtTable(l:getChildByTag(1020), self)
  addSpriteFromBK(l, 1001, nameFromPath(UserData.userinfo.occupant.headpic))
  TypeCast(l:getChildByTag(1011), "CCLabelTTF"):setString(UserData.userinfo.occupant.name)
  --UserData.userinfo.occupant.headpic
  local dur = UserData.userinfo.host.releaseTime - TimeManager.getServerTime()
  local tmp = UITimer.createUITimer("zhanlingcd", dur, self, ccp(0,0), ccp(0, 0), true)
  tmp.view:setPosition(ccp(542, 62))
  self.node:addChild(tmp.view)
  tmp.label:setColor(ccc3(135,7,0))
  self.zhanlingTimer = tmp
end
ZhiHuiBuSceneFunc.setLabels = function(self)
  setBuildingBaseData(BuildingID.ZhiHuiBu, self.node)
  local tmp = 0
  if UserData.userinfo.host.counter.harvestCashTimes~=nil then
    tmp = UserData.userinfo.host.counter.harvestCashTimes
  end
  self.shoushuiCountLabel:setString(string.format("今日收税(%d/%d)", tmp, 10))
  local l = UserData.userinfo.host.cities2[tostring(BuildingID.ZhiHuiBu)].l 
  local levelConfig = UserData.config.models.BaseLevel[l]
  self.curCashLabel:setString(tostring(levelConfig.cashNumber))
  if UserData.config.models.BaseLevel[l+1]~=nil then
    levelConfig = UserData.config.models.BaseLevel[l+1]
    self.nextCashLabel:setString(tostring(levelConfig.cashNumber))
  end 
end

ZhiHuiBuSceneFunc.onUpgradeButton = function(self, node, event)
  if ZhiHuiBuScene.upgradeCooldown==nil or ZhiHuiBuScene.upgradeCooldown<=0 then 
    --self.upBuildingButton:setEnabled(false)
    --setTouchEnabledAll(self.node, false)
    LoadScene:showLoadingScene()
    NetworkRequest.upgradeBuilding(BuildingID.ZhiHuiBu, "onUpgradeZhiHuiBuComplete", self)
    --NetworkRequest.test("onUpgradeZhiHuiBuComplete", self)
  else
    --JiaSuDialog:create(BuildingID.ZhiHuiBu,self) 
  end
  --UpgradeManager.upgradeBuilding(BuildingID.ZhiHuiBu)
end
ZhiHuiBuSceneFunc.onUpgradeZhiHuiBuComplete = function(self, data)
  --setTouchEnabledAll(self.node, true)
  local res = cjson.decode(data)
  print("ZhiHuiBuSceneFunc.onUpgradeZhiHuiBuComplete", data)
  if res.ret==0 then
    NetworkRequest.syncData(res)
    showMessage("升级成功", ccc3(0,255,0))
    --UpgradeManager.upgradeBuilding(BuildingID.ZhiHuiBu)
    local bl = UserData.userinfo.host.cities2[tostring(BuildingID.ZhiHuiBu)].l
    local binfo = UserData.config.models[BuildingLevel[BuildingID.ZhiHuiBu]][bl]
    UserData.userinfo.host.cities2[tostring(BuildingID.ZhiHuiBu)].l = bl+1
    --setBuildingBaseData(BuildingID.ZhiHuiBu, self.node) 
    --self:setLabels()

    self.upBuildingTimer = UITimer.createUITimer(string.format("upbuildingcd_%d", BuildingID.ZhiHuiBu), binfo.coolingTime, self, ccp(160,72), ccp(0, 0))
    self.upBuildingTimer.view:setPosition(ccp(710, 420))
    self.node:addChild(self.upBuildingTimer.view)
    self.timeLabel:setVisible(false)
    self.upBuildingButton:setVisible(false)
    self:checkOpenBuilding()
  else
    showMessage(res.msg, ccc3(255,255,0))
    --self.upBuildingButton:setEnabled(true)
  end
  LoadScene:removeLoadingScene()
  self:closeScene()
  MainScene.scene:addChild(ZhiHuiBuScene:getNode().node)
end
ZhiHuiBuSceneFunc.checkOpenBuilding = function(self)
  local baseLevel = UserData.userinfo.host.cities2["1001"].l
  for k, v in pairs(UserData.config.models.Building2) do
    local b = MainScene.getBuilding(v.id)
    if b~=nil then
      if baseLevel==v.startlevel then
        self:closeScene()
        MainScene.setState() 
        --setTouchEnabledAll(MainScene.node, true)
        if v.id==1002 then
          JunXiaoGuide.nextStep()
        end
      end
    end
  end
end
ZhiHuiBuSceneFunc.onShouShuiButton = function(self, node, event)
  --self.shoushuiButton:setEnabled(false)
  NetworkRequest.shouShui("onShouShuiComplete", self)
  --setTouchEnabledAll(self.node, false)
  LoadScene:showLoadingScene()
  --NetworkRequest.test("onShouShuiComplete", self)
end
ZhiHuiBuSceneFunc.onShouShuiComplete = function(self, data)
  print("ZhiHuiBuSceneFunc.onShouShuiComplete", data)
  local res = cjson.decode(data)
  if res.ret==0 then
    NetworkRequest.syncData(res)
    showMessage(string.format("cash:%d", res.data.gainCash), ccc3(0,255,0))
    --self.shoushuiCountLabel:setString(string.format("今日收税(%d/%d)", UserData.userinfo.host.counter.harvestCashTimes, 10))
    if UserData.userinfo.host.counter.harvestCashTimes==nil then
      UserData.userinfo.host.counter.harvestCashTimes = 0 
    end
    UserData.userinfo.host.counter.harvestCashTimes = UserData.userinfo.host.counter.harvestCashTimes + 1
    self:setLabels()
    local bl = UserData.userinfo.host.cities2[tostring(BuildingID.ZhiHuiBu)].l
    local binfo = UserData.config.models.BaseLevel[bl]
    local tmp = UITimer.createUITimer("shoushuicd", binfo.cashCD, self, ccp(0,0), ccp(0, 0), true)
    tmp.view:setPosition(ccp(357, 150))
    self.node:addChild(tmp.view)
    self.shoushuiTimer = tmp
    self.shoushuiTimeLabel:setVisible(false)
  else
    showMessage(res.msg, ccc3(255,255,0))
    --self.shoushuiButton:setEnabled(true)
  end
  LoadScene:removeLoadingScene()
  self:closeScene()
  MainScene.scene:addChild(ZhiHuiBuScene:getNode().node)
end
ZhiHuiBuSceneFunc.onRevoltButton = function(self, node, event)
  --NetworkRequest.revolt
  self:closeScene()
  local l = CCBReader:load("RevoltDialog.ccbi")
  self.revoltDlg = l
  setObjectExtTable(l:getChildByTag(1111), self)
  setObjectExtTable(l:getChildByTag(1020), self)
  setObjectExtTable(l:getChildByTag(1021), self)
  MainScene.scene:addChild(l)
end
ZhiHuiBuSceneFunc.onRevoltButton1 = function(self, node, event)
  if UserData.userinfo.host.soldiers>0 then
    --setTouchEnabledAll(self.revoltDlg, false)
    LoadScene:showLoadingScene()
    NetworkRequest.revolt(0, "onRevoltComplete1", self)   
  else
    showMessage("兵力不足，请补充兵力！", ccc3(255,255,0))
  end
end
ZhiHuiBuSceneFunc.onRevoltButton2 = function(self, node, event)
  MainScene.scene:removeChild(self.revoltDlg, true)
  local l = CCBReader:load("RevoltDialog1.ccbi")
  self.revoltDlg = l
  setObjectExtTable(l:getChildByTag(1111), self)
  setObjectExtTable(l:getChildByTag(1020), self)
  setObjectExtTable(l:getChildByTag(1021), self)
  MainScene.scene:addChild(l)
end
ZhiHuiBuSceneFunc.onCloseRevoltButton = function(self, node, event)
  MainScene.scene:removeChild(self.revoltDlg, true)
  MainScene.scene:addChild(ZhiHuiBuScene:getNode().node)
end
ZhiHuiBuSceneFunc.onJunXiangButton = function(self, node, event)
  --setTouchEnabledAll(self.revoltDlg, false)
  LoadScene:showLoadingScene() 
  NetworkRequest.revolt(1, "onRevoltComplete2", self)
end
ZhiHuiBuSceneFunc.onJuanZhouButton = function(self, node, event)
  --setTouchEnabledAll(self.revoltDlg, false)
  LoadScene:showLoadingScene()
  NetworkRequest.revolt(2, "onRevoltComplete2", self)
end
ZhiHuiBuSceneFunc.onCloseRevoltButton1 = function(self, node, event)
  print("ZhiHuiBuSceneFunc.onCloseRevoltButton1")
  MainScene.scene:removeChild(self.revoltDlg, true)
  MainScene.scene:addChild(ZhiHuiBuScene:getNode().node)
end
ZhiHuiBuSceneFunc.onRevoltComplete1 = function(self, data)
  print("ZhiHuiBuSceneFunc.onRevoltComplete1", data)
  LoadScene:removeLoadingScene()
  local res = cjson.decode(data)
  if res.ret == 0 then
    NetworkRequest.syncData(res)
    MainScene.scene:removeChild(self.revoltDlg, true)
    local war = WarScene:getNode(UserData.userinfo.occupant.name, self, res)
    MainScene.scene:addChild(war.node)
  else
    --setTouchEnabledAll(self.revoltDlg, true)
    showMessage(res.msg, ccc3(255,255,0))
  end
end
ZhiHuiBuSceneFunc.onRevoltComplete2 = function(self, data)
  print("ZhiHuiBuSceneFunc.onRevoltComplete2", data)
  LoadScene:removeLoadingScene()
  local res = cjson.decode(data)
  if res.ret == 0 then
    NetworkRequest.syncData(res)
    MainScene.scene:removeChild(self.revoltDlg, true)
    MainScene.scene:addChild(ZhiHuiBuScene:getNode().node)
  else
    showMessage(res.msg, ccc3(255,255,0))
    setTouchEnabledAll(self.revoltDlg, true)
  end
end
ZhiHuiBuSceneFunc.fightEnd = function(self)
  MainScene.scene:addChild(ZhiHuiBuScene:getNode().node)
end

ZhiHuiBuSceneFunc.closeScene = function(self)
  UITimer.removeUITimer(self.shoushuiTimer)
  UITimer.removeUITimer(self.upBuildingTimer)
  UITimer.removeUITimer(self.zhanlingTimer)
  MainScene.scene:removeChild(self.node, true)
  ZhiHuiBuScene.instance = nil
end
ZhiHuiBuSceneFunc.onClose = function(self, node, event)
  print("onCloseZhiHuiBu")
  self:closeScene()
  setTouchEnabledAll(MainScene.node, true)
  MainScene.setState()
end

---------------------------------------------------------------------------------------------------------------
--处理加速
ZhiHuiBuSceneFunc.onTimerEnd = function(self, timer)
  if timer~=nil then
    timer.view:getParent():removeChild(timer.view, true)
  end
  if timer==self.shoushuiTimer then
    self.shoushuiButton:setEnabled(true)
    self.shoushuiTimeLabel:setVisible(true)
  elseif timer==self.upBuildingTimer then
    self.timeLabel:setVisible(true)
    self.upBuildingButton:setVisible(true)
    self.upBuildingButton:setEnabled(true)
  elseif timer==self.zhanlingTimer then
    self:checkInvade()    
  end
end

ZhiHuiBuSceneFunc.onJiaSuButton = function(self, timer, pars)
  local tmp = JiaSuSampleDialog:create(pars.leftTime, self)
  MainScene.scene:addChild(tmp.view)
  UITimer.removeUITimer(self.shoushuiTimer)
  UITimer.removeUITimer(self.upBuildingTimer)
  MainScene.scene:removeChild(self.node, true)
  self.jiasuUpBuildingDlg = tmp
end
ZhiHuiBuSceneFunc.onJiaSuConfirm = function(self, dlg, confirm, timereel, timeLeft)
  if confirm~=true then
    MainScene.scene:removeChild(dlg.view, true)
    MainScene.scene:addChild(ZhiHuiBuScene.getNode().node)
  elseif dlg==self.jiasuUpBuildingDlg then
    --NetworkRequest.test("onJiaSuComplete", self)
    NetworkRequest.accelerate("Upgrading", BuildingID.ZhiHuiBu, timereel, "onJiaSuComplete", self)
    --setTouchEnabledAll(dlg.view, false)
    LoadScene:showLoadingScene()
  end
end

ZhiHuiBuSceneFunc.onJiaSuComplete = function(self, data)
  LoadScene:removeLoadingScene()
  local res = cjson.decode(data)
  if res.ret==0 then
    NetworkRequest.syncData(res) 
  --if true then
    if self.jiasuUpBuildingDlg~=nil then
      --MainScene.scene:removeChild(self.jiasuUpBuildingDlg.view, true)
      self.upBuildingTimer:endTimer()
    end
    showMessage("加速成功", ccc3(0,255,0))
  else
    showMessage(res.msg, ccc3(255,255,0))
    --showMessage("加速失败", ccc3(255,255,0))
  end
  if self.jiasuUpBuildingDlg~=nil then
    MainScene.scene:removeChild(self.jiasuUpBuildingDlg.view, true)
  end
  MainScene.scene:addChild(ZhiHuiBuScene.getNode().node)
end
---------------------------------------------------------------------------------------------------------------
